<template>
	<view class="uni-common-mt">
		<view class="uni-form-item uni-column" style="width: 80%; margin:5% auto 0 auto;">
			<view class="title">用户名</view>
			<input class="uni-input" style="border: 1px solid black;" placeholder="输入用户名" v-model="user_name"/>
		</view>
		<view class="uni-form-item uni-column" style="width: 80%; margin:5% auto 0 auto;">
			<view class="title">姓名</view>
			<input class="uni-input" style="border: 1px solid black;" placeholder="输入姓名" v-model="nick_name"/>
		</view>
		<view class="uni-form-item uni-column" style="width: 80%; margin:5% auto 0 auto;">
			<view class="title">密码</view>
			<input class="uni-input"  placeholder="输入密码"  style="border: 1px solid black;" password v-model="password"/>
		</view>
		<view class="uni-form-item uni-column" style="width: 80%; margin:5% auto 0 auto;">
			<view class="title">确认密码</view>
			<input class="uni-input"  placeholder="确认密码"  style="border: 1px solid black;" password v-model="check_password"/>
		</view>
		<view class="a">
			<button @tap="register">注册</button>
		</view>
		<popview4 ref="popview4"></popview4>
		<loading
			ref="loading"
			:custom="false"
			:shadeClick="true"
			:type="1"
			>
			<view style="text-align: center;">等待中</view>
		</loading>
	</view>
</template>

<script>
	import popview4 from '@/components/pop-view/pop-view4.vue'
	import loading from '../../components/xuan-loading/xuan-loading.vue'
	export default {
		components: {
			popview4,
			loading
		  },
		data() {
			return {
				"user_name":"",
				"nick_name":"",
				"password":"",
				"check_password":""
			}
		},
		methods: {
			register(){
				if(this.user_name.length==0){
					this.$refs['popview4'].error('请输入用户名');
				}else if(this.nick_name.length==0){
					this.$refs['popview4'].error('请输入姓名');
				}else if(this.password.length==0){
					this.$refs['popview4'].error('请输入密码');
				}else if(this.password!=this.check_password){
					this.$refs['popview4'].error('两次密码不一致');
				}else{
					this.$refs.loading.open();
					uniCloud.callFunction({
					    name: 'register',
					    data: {
							"user_name":this.user_name,
							"nick_name":this.nick_name,
							"password":this.password,
							"type":"people"
						}
					  })
					  .then(res => {
						  console.log(res)
						  if(res.result.status==2){
							  this.$refs.loading.close();
							   this.$refs['popview4'].success('注册成功');
								setTimeout(()=>{
									uni.reLaunch({
										url:"/pages/login/login"
									})
								},1000)
						  }else{
							  this.$refs.loading.close();
							  this.$refs['popview4'].error('用户名已被使用');
						  }
						
					  });

				}
			}
		}
	}
</script>

<style scoped>
	.a{
		display: flex;
		margin-top: 5%;
	}
	
</style>
